apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ${NAME_SPACE}
  labels:
    app: ${APP_NAME}
  name: ${APP_NAME}-deployment
spec:
  replicas: ${REPLICAS}
  revisionHistoryLimit: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
            maxSurge: 1
            maxUnavailable: 0
  selector:
    matchLabels:
      app: ${APP_NAME}
  template:
    metadata:
      labels:
        app:  ${APP_NAME}
        prometheus.monitor/port: "${PROMETHEUS_PORT}"
        prometheus.monitor/jvm: "true"
    spec:
      imagePullSecrets:
      - name: ${IMAGE_PULL_SECRETS}	
      containers:
      - image: ${IMAGE_URL}/${IMAGE_TAG}
        name: ${APP_NAME}
        imagePullPolicy: Always
        ports:
        - containerPort: ${PORTS}
       # 存活检测
        livenessProbe:
          #如果容器之前探测成功，后续连续几次探测失败，则确定容器未就绪。默认值3，最小值1；
          failureThreshold: 3
          #在初始化容器多少秒后开始第一次就绪探测
          initialDelaySeconds: 30
          #如果Pod未就绪，则每隔多少秒周期性的做就绪探测。默认值10，最小值1；
          periodSeconds: 30
          #如果容器之前探测失败，后续连续几次探测成功，则确定容器就绪。默认值1，最小值1。
          successThreshold: 1
          tcpSocket:
            port: ${PORTS}
          #如果该次就绪探测超过多少秒后还未成功，判定为超时，该次探测失败，Pod不就绪。默认值1，最小值1；
          timeoutSeconds: 1
        # 就绪检测
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 30
          successThreshold: 1
          tcpSocket:
            port: ${PORTS}
          timeoutSeconds: 1
        # 优雅退出
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh","-c","sleep 30"]
        env:
        - name: aliyun_logs_${APP_NAME}
          value: "stdout"
        - name: aliyun_logs_${APP_NAME}_tags
          value: "app=${APP_NAME},env=${VERSION}"
        - name: aliyun_logs_${APP_NAME}-info
          value: "/logs/INFO.*.log"
        - name: aliyun_logs_${APP_NAME}-info_tags
          value: "app=${APP_NAME},env=${VERSION}"
        - name: aliyun_logs_${APP_NAME}-error
          value: "/logs/ERROR.*.log"
        - name: aliyun_logs_${APP_NAME}-error_tags
          value: "app=${APP_NAME},env=${VERSION}"
        # 容器内文件日志路径需要配置emptyDir
        - name: JAVA_OPTS
          value: "${JAVA_OPTS}"
        - name: APOLLO_META
          value: "${APOLLO_META}"
        - name: APP_ID
          value: "${APP_ID}"
        volumeMounts:
        - name: host-time
          mountPath: /etc/localtime
        - name: app-log
          mountPath: /logs
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
        - name: app-log
          emptyDir: {}
      #优化关闭
      terminationGracePeriodSeconds: ${GRACE_PERIOD}
---
apiVersion: v1
kind: Service
metadata:
  namespace: ${NAME_SPACE}
  labels:
  name: ${APP_NAME}-svc
spec:
  selector:
    app: ${APP_NAME}
  ports:
  - port: ${NODE_PORT}
    protocol: TCP
    targetPort: ${PORTS}